package com.goldplusgold.tradedelay.app.home.live;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.base.activity.BaseNetWorkActivity;
import com.goldplusgold.support.lib.base.adapter.BaseListAdapter;
import com.goldplusgold.support.lib.utils.Logger;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshBase;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshListView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.adapter.LiveListAdapter;
import com.goldplusgold.tradedelay.app.home.live.adapter.VideoListAdatper;

/**
 * Created by Administrator on 2017/7/10.
 */

public class PullListActivity extends BaseNetWorkActivity implements View.OnClickListener {

    ImageView backView;
    TextView titleView;
    String title;
    String adapterPath = "";
    PullToRefreshListView listView;
    BaseListAdapter listAdapter;
    boolean isUp = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getBundle();
        setAdapterClass();
    }

    private void getBundle(){
        Intent intent = this.getIntent();
        if(intent != null){
            title = intent.getStringExtra("title");
            adapterPath = intent.getStringExtra("adapter");
            isUp = intent.getBooleanExtra("isUp",false);
        }
        refreshView();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initView(){
        listView = (PullToRefreshListView) this.findViewById(R.id.base_list);
        backView = (ImageView) this.findViewById(R.id.iv_back);
        titleView = (TextView) this.findViewById(R.id.title_txt);
        backView.setVisibility(View.VISIBLE);
        backView.setOnClickListener(this);
        listView.getRefreshableView().setHeaderDividersEnabled(false);
        listView.getRefreshableView().setBackgroundColor(this.getResources().getColor(R.color.act_bg));
        listView.getLoadingLayoutProxy().setPullLabel("");
        listView.getLoadingLayoutProxy().setRefreshingLabel("");
        listView.getLoadingLayoutProxy().setReleaseLabel("");
        listView.getLoadingLayoutProxy().setLastUpdatedLabel("");
        listView.getRefreshableView().setDividerHeight(this.getResources().getDimensionPixelSize(R.dimen.list_video_item_margin_bottom));
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if(listAdapter != null){
                    listAdapter.downRereshView();
                }
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if(listAdapter != null){
                    listAdapter.upRereshView();
                }
            }
        });
    }

    private void refreshView(){
        if(title != null){
            titleView.setVisibility(View.VISIBLE);
            titleView.setText(title);
        }
        if(isUp){
            listView.setMode(PullToRefreshBase.Mode.BOTH);
        }
    }

    private void setAdapterClass(){
        if(adapterPath != null){
            try {
                Class adapter = this.getClass().getClassLoader().loadClass(adapterPath);
                if(adapter != null){
                    listAdapter = (BaseListAdapter) adapter.newInstance();
                    if(listAdapter != null){
                        listAdapter.setContext(this);
                        listView.setVisibility(View.VISIBLE);
                        listView.setAdapter(listAdapter);
                        listAdapter.setPullView(listView);
                        listAdapter.requestData();
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                Logger.e("error",e.toString());
            } catch (InstantiationException e) {
                e.printStackTrace();
                Logger.e("error",e.toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Logger.e("error",e.toString());
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        listAdapter = null;
    }

    @Override
    public void onClick(View v) {
        if(v == backView){
            this.finish();
        }
    }

}
